package com.zhang.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.domain.Manager;
import com.zhang.domain.Product;
import com.zhang.domain.ProductDetail;
import com.zhang.domain.Shop;
import com.zhang.mapper.ProductDetailMapper;
import com.zhang.mapper.ProductMapper;
import com.zhang.query.ProductQuery;
import com.zhang.service.IProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zh
 * @create 2022-03-07 10:30
 */
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
@Service
public class ProductServiceImpl implements IProductService {
    @Resource
    private ProductMapper mapper;
    @Resource
    private ProductDetailMapper detailMapper;
    @Transactional
    @Override
    public void save(Product p) {
        p.setResources("http://34.80.47.178/"+p.getResources());
        p.setCreatetime(new Date());
        mapper.save(p);
        ProductDetail productDetail = p.getProductDetail();
        productDetail.setProduct_id(p.getId());
        detailMapper.save(productDetail);

    }
    @Transactional
    @Override
    public void update(Product p) {
        if(!p.getResources().contains("http://34.80.47.178/")){
            p.setResources("http://34.80.47.178/"+p.getResources());
        }
        mapper.update(p);
        ProductDetail productDetail = p.getProductDetail();
        productDetail.setProduct_id(p.getId());
        detailMapper.save(productDetail);

    }
    @Transactional
    @Override
    public void delete(Long id) {
        mapper.delete(id);
        detailMapper.delete(id);
    }

    @Override
    public PageInfo<Product> queryData(ProductQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Product> products = mapper.queryData(query);
        Page<Product> page = (Page<Product>) products;
        return page.toPageInfo();
    }
    @Transactional
    @Override
    public void deleteAll(Long[] id) {
        mapper.deleteAll(id);
        detailMapper.deleteAll(id);
    }

}
